package com.demo.controller;

import com.demo.domain.SysMenu;
import com.demo.model.R;
import com.demo.model.RolePrivilegesParam;
import com.demo.service.SysRolePrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@Api(value = "角色权限的配置")
public class SysRolePrivilegeController {

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @GetMapping("/roles_privileges")
    @ApiOperation("查询角色的权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value ="roleId" ,name ="角色ID" )
    })
    public R<List<SysMenu>> findSysMenuAndPrivilege(Long roleId){
       List<SysMenu> menus = sysRolePrivilegeService.findSysMenuAndPrivilege(roleId);
        return R.ok(menus);
    }

    /**
     * 根据角色的权限列表 修改角色权限
     * @param rolePrivilegesParam
     * @return
     */
    @PostMapping("/grant_privileges")
    @ApiOperation("授予角色某种权限")
    public R grantPrivileges(@RequestBody RolePrivilegesParam rolePrivilegesParam){
        boolean isOk = sysRolePrivilegeService.grantPrivileges(rolePrivilegesParam);
        if (isOk){
            return R.ok();
        }
        return  R.fail();
    }
}
